//
//  RTUIBarButtonItem+Extension.swift
//  Weibo
//
//  Created by Brisinga on 16/3/2.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit


extension UIBarButtonItem {

    convenience init(imageNamed: String?=nil,title: String?=nil,target: AnyObject?, action: Selector) {
        
        self.init()
        
        let btn = UIButton()
        
        // 添加监听事件
        btn .addTarget(target, action: action, forControlEvents: .TouchUpInside)
        
        // 如果title存在设置title
        if let t = title {
            
            btn.setTitle(t, forState: .Normal)
            
            btn.setTitleColor(UIColor(colorLiteralRed: 52/255.0, green: 52/255.0, blue: 52/255.0, alpha: 1), forState: .Normal)
            
            btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            
            btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        }
        
        
        // 如果图片存在设置图片
        if let imgName = imageNamed {
            
            btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
            
            btn.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        }
        
        btn.sizeToFit()
        
        customView = btn
    }
}


